<template>
  <div class="page-login">
    <view v-if="step === 1" class="step step1">
      <TopInfo title="第一步" desc="请输入家长名称" />
      <view class="container">
        <button class="post" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
          <image mode="aspectFill" :src="form.avatarUrl" />
        </button>
        <input
          v-model="form.nickName"
          type="nickname"
          class="name"
          placeholder="请输入昵称"
        >
      </view>
      <button class="add-btn" @click="next">
        下一步
      </button>
    </view>
    <view v-if="step === 2" class="step step2">
      <TopInfo title="第二步" desc="请输入子女名称" />
      <view class="container">
        <input
          v-model="form.childName"
          class="name"
          placeholder="请输入姓名"
        >
      </view>
      <button class="prev-btn" @click="step = 1">
        上一步
      </button>
      <button class="add-btn" @click="submit">
        提交
      </button>
    </view>
  </div>
</template>

<script lang="ts" setup>
import type { IRegisterForm } from '@/api/login'
import { useTokenStore } from '@/store/token'
import TopInfo from './components/TopInfo.vue'

defineOptions({
  name: 'Login',
})

definePage({
  style: {
    navigationStyle: 'custom',
    backgroundColor: '#fff',
  },
})

const step = ref(1)
const form = reactive<Omit<IRegisterForm, 'code'>>({
  avatarUrl: '',
  nickName: '',
  childName: '',
  fileId: '',
})
// #region Props
// #endregion
// #region Emits
// #endregion
// #region Hooks
// #endregion
// #region Computed
// #endregion
// #region Watch
// #endregion
// #region Event
// #endregion
// #region Function
function next() {
  if (!form.avatarUrl) {
    return uni.showToast({
      icon: 'none',
      title: '请选择头像',
    })
  }
  if (!form.nickName) {
    return uni.showToast({
      icon: 'none',
      title: '请设置家长名称',
    })
  }
  step.value = 2
}

function onChooseAvatar(e: any) {
  const { avatarUrl } = e.detail
  const name = avatarUrl.split('tmp/').pop()
  uniCloud.uploadFile({
    filePath: avatarUrl,
    cloudPath: name,
    success: (res) => {
      form.avatarUrl = avatarUrl
      form.fileId = res.fileID
    },
  })
}

function submit() {
  if (!form.childName) {
    return uni.showToast({
      icon: 'none',
      title: '请设置子女名称',
    })
  }

  useTokenStore().register(toRaw(form)).then(() => {
    uni.switchTab({
      url: '/pages/index/index',
    })
  })
}
// #endregion
// #region Life Cycle
// #endregion
// #region Expose
// #endregion
</script>

<style lang="scss" scoped>
.page-login {
  height: 100%;
  padding-top: 20%;
  box-sizing: border-box;
  .step {
    display: flex;
    flex-direction: column;
    height: 100%;
    box-sizing: border-box;
    padding: 0 20px calc(env(safe-area-inset-bottom) + 20px) 20px;
    .post {
      width: 160px;
      height: 160px;
      border-radius: 160px;
      flex: 0 0 auto;
      padding: 0;
      background: url('https://mp-faaaa8a0-2641-45b8-8dbc-bcb0d1463eb4.cdn.bspapp.com/app/icon-camera.png') no-repeat
        center rgba(0, 0, 0, 0.08);
      background-size: 60px 60px;
      image {
        width: 100%;
        height: 100%;
      }
    }
    .container {
      display: flex;
      flex-direction: column;
      flex: 1 0 auto;
      height: 50%;
      margin-top: 40px;
    }
  }
  .name {
    height: 60px;
    border-radius: 10px;
    text-align: center;
    color: var(--text-color);
    background: var(--bg-color);
    font-size: 16px;
    margin-top: 40px;
    border: 2px solid rgba(0, 0, 0, 0.01);
  }
  .prev-btn {
    height: 60px;
    margin: 0 auto;
    width: 100%;
    border-radius: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(0, 0, 0, 0.5);
    background: none;
    margin-bottom: 20px;
  }
  .add-btn {
    height: 60px;
    background: var(--button-color);
    margin: 0 auto;
    width: 100%;
    border-radius: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
